package com.yiren.manager.ctrl.elder;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrElder;
import com.yiren.entity.YrElderMemContact;
import com.yiren.entity.YrStaffInfo;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.elder.YrElderForm;
import com.yiren.manager.form.elder.YrElderMemContactForm;
import com.yiren.manager.service.iface.department.YrStaffInfoService;
import com.yiren.manager.service.iface.elder.YrElderMemContactService;
import com.yiren.manager.service.iface.elder.YrElderService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("elderMemContacts")
@Controller
public class YrElderMemContactController extends GenericController<YrElderMemContactForm, YrElderMemContact>{

	@Autowired
	private YrElderMemContactService selfService;
	
	@Autowired
	private YrStaffInfoService staffInfoService;
	
	@Autowired
	private YrElderService elderService;
	
	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrElderMemContactForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrElderMemContactForm> result = new HTTPResult<YrElderMemContactForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrElderMemContactForm form = tool.mapper("elderMemContacts", YrElderMemContactForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getElderMemContactsId()||form.getElderMemContactsId().isEmpty()){
			form.setElderMemContactsId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		YrElderMemContact entity = ObjectUtils.objectToObject(form, YrElderMemContact.class);
		selfService.save(entity);
		
		return result;
	}
	
	@RequestMapping("find")
	@ResponseBody
	public HTTPResult<YrElderMemContactForm> find(String elderId) throws Exception{
		HTTPResult<YrElderMemContactForm> result = new HTTPResult<YrElderMemContactForm>();
		String sqlWhere = "elder_id=?1";
		
		QueryResult<YrElderMemContact> qr = selfService.find(YrElderMemContact.class, sqlWhere, new Object[]{elderId});
		List<YrElderMemContactForm> formList = new ArrayList<YrElderMemContactForm>();
		
		for(YrElderMemContact entity : qr.getResultList()){
			YrElderMemContactForm form = toForm(entity);
			if(null!=form.getCreateId()){
				YrStaffInfo staffInfo = staffInfoService.findOne(form.getCreateId());
				if(null!=staffInfo){
					form.setRecorder(staffInfo.getName());
				}
			}
			YrElder elder = elderService.findOne(elderId);
			form.setElderForm(ObjectUtils.objectToObject(elder, YrElderForm.class));
			formList.add(form);
		}
		
		result.setData(formList);
		return result;
	}
	
	
	@Override
	public YrElderMemContactForm toForm(YrElderMemContact e) throws Exception {
		YrElderMemContactForm form = ObjectUtils.objectToObject(e, YrElderMemContactForm.class);
		return form;
	}

	@Override
	public YrElderMemContact toEntity(YrElderMemContactForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
